<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\CountStudentModel;
use App\Models\CountOrderModel;
use App\Models\CountCourseModel;
use App\Models\CountIncomeModel;

class CountController extends Controller
{
    public function total(Request $request){
        // 总学生 总订单量 总收入 总课时
        $where = [
            'company_id' => $request->company_id,
            'year' => 0,
            'month' => 0,
            'finish' => 0
        ];
        $totalStudent = CountStudentModel::where($where)->first();
        $totalOrder = CountOrderModel::where($where)->first();
        $totalIncome = CountIncomeModel::where($where)->first();
        $totalCourse = CountCourseModel::where($where)->first();

        return self::ok([
            'student' => $totalStudent->num?? 0,
            'order' => $totalOrder->num?? 0,
            'income' => $totalIncome->num?? 0,
            'course' => $totalCourse->num?? 0
        ]);
    }

    public function percent(Request $request){
        $where = [
            'company_id' => $request->company_id,
            'year' => 0,
            'month' => 0,
            'finish' => 0
        ];
        $totalStudent = CountStudentModel::where($where)->first();
        $totalIncome = CountIncomeModel::where($where)->first();
        $totalCourse = CountCourseModel::where($where)->first();

        $where['finish'] = 1;
        $finishedStudent = CountStudentModel::where($where)->first();
        $finishedIncome = CountIncomeModel::where($where)->first();
        $finishedCourse = CountCourseModel::where($where)->first();
        return self::ok([
            'studentAll'    => $totalStudent->num?? 0,
            'studentFinish' => $finishedStudent->num?? 0,
            'incomeAll'     => $totalIncome->num?? 0,
            'incomeFinish'  => $finishedIncome->num?? 0,
            'courseAll'     => $totalCourse->num?? 0,
            'courseFinish'  => $finishedCourse->num?? 0
        ]);
    }

    /**
     * 获取最近十二个月的月度收益
     */
    public function month_income(Request $request) {
        $totalIncome = CountIncomeModel::where([
            'company_id' => $request->company_id,
            'year' => 0,
            'month' => 0,
            'finish' => 0
        ])->first();

        $monthIncome = CountIncomeModel::where('company_id', $request->company_id)
            ->where('finish', 0)    
            ->where('month', '>', 0)
            ->orderBy('year', 'desc')
            ->orderBy('month', 'desc')
            ->limit(12)
            ->get();
        return self::ok([
            'total' => $totalIncome->num?? 0,
            'month' => $monthIncome
        ]);
    }

    /**
     * 获取最近十二个月的月度销课量
     */
    public function month_course(Request $request) {
        $monthCourse = CountCourseModel::where('company_id', $request->company_id)
            ->where('month', '>', 0)
            ->where('finish', 1)
            ->orderBy('year', 'desc')
            ->orderBy('month', 'desc')
            ->limit(12)
            ->get();
        return self::ok($monthCourse);
    }
}
